package net.zjitc.seqqueue;

import net.zjitc.exception.MyQuException;

import java.util.LinkedList;

/**
 * Created by Administrator on 2017/3/30.
 */
public class SeqQueue<E> implements IQueue<E>{
    LinkedList<E> list=new LinkedList();
    private int maxsize;

    public SeqQueue(int maxsize) {
        this.maxsize = maxsize;
    }

    //入队列操作
    @Override
    public void enqueue(E item) throws MyQuException {
        //最大元素个数确定后，在增加时需要判断队列是否为满
        if (!isFull()){
            list.addLast(item);
        }else {
            throw new MyQuException("队列已满");
        }

    }
    //出队列操作
    @Override
    public E dequeue() throws MyQuException {
        //在删除时需要判断队列是否为空
        if (!isEmpty()){
           return list.removeFirst();
        }else{
            throw new MyQuException("队列已空");
        }
    }
    //取对头元素
    @Override
    public E peek() {
        return list.getFirst();
    }
    // 求队列的长度
    @Override
    public int size() {
        return list.size();
    }
    //判断队列是否为空
    @Override
    public boolean isEmpty() {
        return list.isEmpty();
    }
    //判断队列是否为满
    @Override
    public boolean isFull() {
        if (list.size()==maxsize){
            return true;
        }
        return false;
    }
}
